package com.chenfan.filestore.service.biz.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfan.ccp.common.exception.ApiException;
import com.chenfan.filestore.dto.BatchExportTemplateDTO;
import com.chenfan.filestore.dto.BatchExportTemplateQueryDTO;
import com.chenfan.filestore.exception.FileStoreCode;
import com.chenfan.filestore.model.BatchExportTemplate;
import com.chenfan.filestore.service.BatchExportTemplateService;
import com.chenfan.filestore.service.biz.BatchExportTemplateBiz;
import com.chenfan.filestore.vo.BatchExportTemplateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author K
 * @date 2023/4/26 5:46 PM
 */
@Service
public class BatchExportTemplateBizImpl implements BatchExportTemplateBiz {

    @Autowired
    private BatchExportTemplateService batchExportTemplateService;

    @Override
    public Long createBatchExportTemplate(BatchExportTemplateDTO batchExportTemplateDTO) {
        String templateCode = batchExportTemplateDTO.getTemplateCode();
        // 查询对应的模板编号是否存在
        BatchExportTemplate checkTemplate = batchExportTemplateService.findByTemplateCode(templateCode);
        if (Objects.nonNull(checkTemplate)) {
            throw new ApiException(FileStoreCode.TEMPLATE_CODE_EXISTS);
        }

        BatchExportTemplate exportTemplate = BatchExportTemplate.ofDefault(batchExportTemplateDTO);
        batchExportTemplateService.save(exportTemplate);

        return exportTemplate.getTemplateId();
    }

    @Override
    public void updateBatchExportTemplate(BatchExportTemplateDTO batchExportTemplateDTO) {
        BatchExportTemplate template = getAndCheck(batchExportTemplateDTO.getTemplateId());
        BeanUtils.copyProperties(batchExportTemplateDTO, template);
        template.updateSelf();
        batchExportTemplateService.updateById(template);
    }

    @Override
    public Page<BatchExportTemplateVO> pageBatchExportTemplate(BatchExportTemplateQueryDTO query) {
        Page<BatchExportTemplateVO> batchExportTemplateVOPage = new Page<>(query.getPageSize(), query.getPageNum());
        Page<BatchExportTemplate> batchExportTemplatePage = batchExportTemplateService.pageBatchExportTemplate(query);

        List<BatchExportTemplateVO> records = batchExportTemplatePage.getRecords().stream().map(e -> {
            BatchExportTemplateVO batchExportTemplateVO = new BatchExportTemplateVO();
            BeanUtils.copyProperties(e, batchExportTemplateVO);
            return batchExportTemplateVO;
        }).collect(Collectors.toList());

        batchExportTemplateVOPage.setRecords(records);
        batchExportTemplateVOPage.setTotal(batchExportTemplatePage.getTotal());
        batchExportTemplateVOPage.setCurrent(batchExportTemplatePage.getCurrent());
        return batchExportTemplateVOPage;
    }

    @Override
    public BatchExportTemplateVO getBatchExportTemplate(Long templateId) {
        BatchExportTemplate template = getAndCheck(templateId);
        BatchExportTemplateVO templateVO = new BatchExportTemplateVO();
        BeanUtils.copyProperties(template, templateVO);
        return templateVO;
    }

    @Override
    public void switchStatus(Long templateId, Integer status) {
        BatchExportTemplate batchExportTemplate = getAndCheck(templateId);
        if ((batchExportTemplate.getStatus() ^ status) == 0) {
            throw new ApiException(FileStoreCode.STATUS_NOT_CHANGE);
        }
        batchExportTemplate.setStatus(status);
        batchExportTemplate.updateSelf();

        batchExportTemplateService.updateById(batchExportTemplate);
    }

    private BatchExportTemplate getAndCheck(Long templateId) {
        BatchExportTemplate template = batchExportTemplateService.getById(templateId);
        if (Objects.isNull(template)) {
            throw new ApiException(FileStoreCode.TEMPLATE_DOES_NOT_EXISTS);
        }
        return template;
    }
}
